package com.netintech.project.system.user.controller;

import com.netintech.core.web.domain.RequestResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


/**
 * 授权控制器
 * @author admin
 */
@Controller
@RequestMapping("/auth")
public class AuthController {

    /**
     * 上传授权文件页面
     * @return
     */
    @GetMapping("/uploadpage")
    public String uploadAuthFile(){
        return "upload_auth";
    }


    /**
     * 上传授权文件
     */
    @PostMapping("upload")
    @ResponseBody
    public RequestResult uploadFile(@RequestParam("file_data") MultipartFile[] file)
    {
        try
        {
            // 上传文件路径
            String filePath = new File("").getCanonicalPath();
            File oldFile = new File(filePath+"/a.licence");
            if(oldFile.exists()){
                oldFile.delete();
            }
            MultipartFile file1 = file[0];
            File desc = new File(filePath + File.separator + file1.getOriginalFilename());
            if (!desc.getParentFile().exists())
            {
                desc.getParentFile().mkdirs();
            }
            if (!desc.exists())
            {
                desc.createNewFile();
            }
            file1.transferTo(desc);
            RequestResult ajax = RequestResult.success();
            return ajax;
        }
        catch (Exception e)
        {
            return RequestResult.error(e.getMessage());
        }
    }

    /**
     * 上传授权文件
     */
    @PostMapping("/removeFile")
    @ResponseBody
    public RequestResult remove() throws Exception {
        // 上传文件路径
        String filePath = new File("").getCanonicalPath();
        File oldFile = new File(filePath+"/a.licence");
        if(oldFile.exists()){
            oldFile.delete();
        }
        return RequestResult.success();
    }
}
